package com.example.keyboardedittext.keyboard;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.example.keyboardedittext.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by gongshuobo on 2016/11/18.
 * 英语键盘View
 */

public class KeyBoardEnglishView extends LinearLayout {

    private LinearLayout englishLetterView;
    private View englishLetterSmallView;
    private View englishLetterCaseView;
    private View englishSignView;

    private TextView englishNavigationLetter;
    private TextView englishNavigationSign;

    private KeyBoardCallBack keyBoardCallBack;

    private Map<Integer,String> keyBoardEnglishMap;

    private int[] englishSignKey = {
            R.id.tv_keyboard_sign_comma,
            R.id.tv_keyboard_sign_period,
            R.id.tv_keyboard_sign_question,
            R.id.tv_keyboard_sign_sigh,
            R.id.tv_math_number_colon,
            R.id.tv_keyboard_sign_semicolon,
            R.id.tv_math_number_point,
            R.id.tv_number_subtraction,
            R.id.tv_keyboard_sign_last_quotes,
            R.id.tv_keyboard_sign_last_single,
            R.id.tv_math_division_less,
            R.id.tv_math_division_more,
            R.id.tv_math_division_left_bracket,
            R.id.tv_math_division_right_bracket,
            R.id.tv_math_division_left_line,
            R.id.tv_math_division_right_line,
            R.id.tv_sign_a,
            R.id.tv_sign_dollar,
            R.id.tv_math_number_percentage,
            R.id.tv_sign_and,
            R.id.tv_sign_bottom_line};
    private int[] englishSignValue = {
            R.string.keyboard_sign_comma,
            R.string.keyboard_sign_period,
            R.string.keyboard_sign_question,
            R.string.keyboard_sign_sigh,
            R.string.keyboard_edit_maohao,
            R.string.keyboard_sign_semicolon,
            R.string.keyboard_sign_point_chinese,
            R.string.keyboard_sign_subtraction,
            R.string.keyboard_sign_last_quotes,
            R.string.keyboard_sign_ver_single,
            R.string.keyboard_sign_less,
            R.string.keyboard_sign_more,
            R.string.keyboard_sign_left_bracket,
            R.string.keyboard_sign_right_bracket,
            R.string.keyboard_sing_left_line,
            R.string.keyboard_sing_right_line,
            R.string.keyboard_a,
            R.string.keyboard_dollar,
            R.string.keyboard_edit_percentage,
            R.string.keyboard_and,
            R.string.keyboard_small_bottom_line};
    /**
     * 英语键盘小写字母对应的id和值映射
     **/
    private int[] englishLetterSmallKey = {R.id.tv_math_letter_a, R.id.tv_math_letter_b, R.id.tv_math_letter_c, R.id.tv_math_letter_d, R.id.tv_math_letter_e, R.id.tv_math_letter_f, R.id.tv_math_letter_g, R.id.tv_math_letter_h, R.id.tv_math_letter_i, R.id.tv_math_letter_j, R.id.tv_math_letter_k, R.id.tv_math_letter_l, R.id.tv_math_letter_m, R.id.tv_math_letter_n, R.id.tv_math_letter_o, R.id.tv_math_letter_p, R.id.tv_math_letter_q, R.id.tv_math_letter_r, R.id.tv_math_letter_s, R.id.tv_math_letter_t, R.id.tv_math_letter_u, R.id.tv_math_letter_v, R.id.tv_math_letter_w, R.id.tv_math_letter_x, R.id.tv_math_letter_y, R.id.tv_math_letter_z};
    private int[] mathLetterSmallValue = {R.string.keyboard_sign_a, R.string.keyboard_sign_b, R.string.keyboard_sign_c, R.string.keyboard_sign_d, R.string.keyboard_sign_e, R.string.keyboard_sign_f, R.string.keyboard_sign_g, R.string.keyboard_sign_h, R.string.keyboard_sign_i, R.string.keyboard_sign_j, R.string.keyboard_sign_k, R.string.keyboard_sign_l, R.string.keyboard_sign_m, R.string.keyboard_sign_n, R.string.keyboard_sign_o, R.string.keyboard_sign_p, R.string.keyboard_sign_q, R.string.keyboard_sign_r, R.string.keyboard_sign_s, R.string.keyboard_sign_t, R.string.keyboard_sign_u, R.string.keyboard_sign_v, R.string.keyboard_sign_w, R.string.keyboard_sign_x, R.string.keyboard_sign_y, R.string.keyboard_sign_z};
    /**
     * 英语键盘大写字母对应的id和值映射
     **/
    private int[] englishLetterCaseKey = {R.id.tv_math_letter_A, R.id.tv_math_letter_B, R.id.tv_math_letter_C, R.id.tv_math_letter_D, R.id.tv_math_letter_E, R.id.tv_math_letter_F, R.id.tv_math_letter_G, R.id.tv_math_letter_H, R.id.tv_math_letter_I, R.id.tv_math_letter_J, R.id.tv_math_letter_K, R.id.tv_math_letter_L, R.id.tv_math_letter_M, R.id.tv_math_letter_N, R.id.tv_math_letter_O, R.id.tv_math_letter_P, R.id.tv_math_letter_Q, R.id.tv_math_letter_R, R.id.tv_math_letter_S, R.id.tv_math_letter_T, R.id.tv_math_letter_U, R.id.tv_math_letter_V, R.id.tv_math_letter_W, R.id.tv_math_letter_X, R.id.tv_math_letter_Y, R.id.tv_math_letter_Z};
    private int[] englishLetterCaseValue = {R.string.keyboard_sign_capital_a, R.string.keyboard_sign_capital_b, R.string.keyboard_sign_capital_c, R.string.keyboard_sign_capital_d, R.string.keyboard_sign_capital_e, R.string.keyboard_sign_capital_f, R.string.keyboard_sign_capital_g, R.string.keyboard_sign_capital_h, R.string.keyboard_sign_capital_i, R.string.keyboard_sign_capital_j, R.string.keyboard_sign_capital_k, R.string.keyboard_sign_capital_l, R.string.keyboard_sign_capital_m, R.string.keyboard_sign_capital_n, R.string.keyboard_sign_capital_o, R.string.keyboard_sign_capital_p, R.string.keyboard_sign_capital_q, R.string.keyboard_sign_capital_r, R.string.keyboard_sign_capital_s, R.string.keyboard_sign_capital_t, R.string.keyboard_sign_capital_u, R.string.keyboard_sign_capital_v, R.string.keyboard_sign_capital_w, R.string.keyboard_sign_capital_x, R.string.keyboard_sign_capital_y, R.string.keyboard_sign_capital_z};

    private int[] englishLetterKey = {R.id.tv_keyboard_sign_comma,R.id.tv_keyboard_sign_period,R.id.tv_keyboard_sign_question,R.id.tv_keyboard_sign_sigh,R.id.tv_math_number_colon,R.id.tv_keyboard_sign_last_quotes,R.id.tv_keyboard_sign_next_quotes,R.id.tv_keyboard_sign_next_single,R.id.tv_math_division_left_line,R.id.tv_number_subtraction};
    private int[] englishLetterValue = {R.string.keyboard_sign_comma,R.string.keyboard_sign_period,R.string.keyboard_sign_question,R.string.keyboard_sign_sigh,R.string.keyboard_edit_maohao,R.string.keyboard_sign_last_quotes,R.string.keyboard_sign_next_quotes,R.string.keyboard_sign_ver_single,R.string.keyboard_sing_left_line,R.string.keyboard_sign_subtraction};


    public KeyBoardEnglishView(Context context) {
        super(context);
        initKeyBoardEnglishView();
        initKeyBoardEnglishMap();
        initKeyBoardEnglishOnClick();
    }

    private void initKeyBoardEnglishOnClick() {
        for(int englishSignId:englishSignKey){
            englishSignView.findViewById(englishSignId).setOnClickListener(keyboardEnglishOnClick);
        }
        for(int i=0;i<englishLetterSmallKey.length;i++){
            englishLetterSmallView.findViewById(englishLetterSmallKey[i]).setOnClickListener(keyboardEnglishOnClick);
            englishLetterCaseView.findViewById(englishLetterCaseKey[i]).setOnClickListener(keyboardEnglishOnClick);
        }
        for (int anEnglishLetterKey : englishLetterKey) {
            englishLetterView.findViewById(anEnglishLetterKey).setOnClickListener(keyboardEnglishOnClick);
        }
        englishSignView.findViewById(R.id.tv_keyboard_delete).setOnClickListener(keyboardEnglishOnClick);
        englishSignView.findViewById(R.id.tv_keyboard_delete).setOnLongClickListener(keyboardChineseOnLongClick);
        englishLetterSmallView.findViewById(R.id.tv_keyboard_delete).setOnClickListener(keyboardEnglishOnClick);
        englishLetterSmallView.findViewById(R.id.tv_keyboard_delete).setOnLongClickListener(keyboardChineseOnLongClick);
        englishLetterSmallView.findViewById(R.id.tv_keyboard_small).setOnClickListener(keyboardEnglishNavigationOnClick);
        englishLetterCaseView.findViewById(R.id.tv_keyboard_delete).setOnClickListener(keyboardEnglishOnClick);
        englishLetterCaseView.findViewById(R.id.tv_keyboard_delete).setOnLongClickListener(keyboardChineseOnLongClick);
        englishLetterCaseView.findViewById(R.id.tv_keyboard_case).setOnClickListener(keyboardEnglishNavigationOnClick);
    }

    private void initKeyBoardEnglishMap() {
        keyBoardEnglishMap = new HashMap<Integer, String>();
        for(int i=0;i<englishSignKey.length;i++){
            keyBoardEnglishMap.put(englishSignKey[i],getResources().getString(englishSignValue[i]));
        }
        for(int i=0;i<englishLetterCaseKey.length;i++){
            keyBoardEnglishMap.put(englishLetterCaseKey[i],getResources().getString(englishLetterCaseValue[i]));
            keyBoardEnglishMap.put(englishLetterSmallKey[i],getResources().getString(mathLetterSmallValue[i]));
        }
        for(int i=0;i<englishLetterKey.length;i++){
            keyBoardEnglishMap.put(englishLetterKey[i],getResources().getString(englishLetterValue[i]));
        }
    }

    private void initKeyBoardEnglishView() {
        englishLetterView = (LinearLayout) getLayoutView(R.layout.keyboard_english_letter);
        englishLetterSmallView = getLayoutView(R.layout.keyboard_math_letter_small);
        englishLetterCaseView = getLayoutView(R.layout.keyboard_math_letter_case);
        englishSignView = getLayoutView(R.layout.keyboard_english_sign);
        addView(englishLetterView,getKeyBoardContentParams());
        addView(englishSignView,getKeyBoardContentParams());
        LinearLayout layoutLetter = (LinearLayout)englishLetterView.findViewById(R.id.layout_letter);
        layoutLetter.addView(englishLetterSmallView,getKeyBoardContentParams());
        layoutLetter.addView(englishLetterCaseView,getKeyBoardContentParams());
    }

    /**
     * 英语两种键盘类型绑定事件
     */

    OnClickListener keyboardEnglishNavigationOnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.tv_english_sign:
                    englishSignView.setVisibility(View.VISIBLE);
                    englishLetterView.setVisibility(View.GONE);
                    englishNavigationSign.setTextColor(getResources().getColor(R.color.keyboard_black));
                    englishNavigationLetter.setTextColor(getResources().getColor(R.color.keyboard_white));
                    englishNavigationSign.setBackgroundResource(R.drawable.keyboard_item_navigation);
                    englishNavigationLetter.setBackgroundResource(R.drawable.keyboard_item_navigation_pressed);
                    break;
                case R.id.tv_english_letter:
                    englishSignView.setVisibility(View.GONE);
                    englishLetterView.setVisibility(View.VISIBLE);
                    englishNavigationSign.setBackgroundResource(R.drawable.keyboard_item_navigation_pressed);
                    englishNavigationSign.setTextColor(getResources().getColor(R.color.keyboard_white));
                    englishNavigationLetter.setTextColor(getResources().getColor(R.color.keyboard_black));
                    englishNavigationLetter.setBackgroundResource(R.drawable.keyboard_item_navigation);
                    break;
                case R.id.tv_keyboard_small:
                    englishLetterSmallView.setVisibility(View.GONE);
                    englishLetterCaseView.setVisibility(View.VISIBLE);
                    break;
                case R.id.tv_keyboard_case:
                    englishLetterCaseView.setVisibility(View.GONE);
                    englishLetterSmallView.setVisibility(View.VISIBLE);
                    break;
            }

        }
    };


    /**
     * 公共按键(删除,每个按键的事件)数学键盘点击事件
     **/
    OnClickListener keyboardEnglishOnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (keyBoardCallBack != null) {
                switch (v.getId()) {
                    case R.id.tv_keyboard_delete:
                        keyBoardCallBack.deleteKeyBoard();
                        break;
                    case R.id.tv_keyboard_molecular:
                        keyBoardCallBack.showMolecular();
                        break;
                    default:
                        String mathNumberValue = keyBoardEnglishMap.get(v.getId());
                        keyBoardCallBack.getFinishEditValue(mathNumberValue);
                }
            }
        }
    };

    /**
     * 公共按键(长按删除)数学键盘长按事件
     **/
    OnLongClickListener keyboardChineseOnLongClick = new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            switch (v.getId()) {
                case R.id.tv_keyboard_delete:
                    if (keyBoardCallBack != null) {
                        keyBoardCallBack.deleteLongKeyBoard();
                    }
                    break;
            }
            return false;
        }
    };

    /**
     * 设置数学按键的绑定事件
     **/
    public void setOnClickKeyFinishListener(KeyBoardCallBack keyBoardCallBack) {
        this.keyBoardCallBack = keyBoardCallBack;
    }

    /**
     * 绑定导航栏的按钮
     */

    public void setNavigationSelected(View keyBoardNavigationEnglish){
        this.englishNavigationLetter = (TextView) keyBoardNavigationEnglish.findViewById(R.id.tv_english_letter);
        this.englishNavigationSign = (TextView) keyBoardNavigationEnglish.findViewById(R.id.tv_english_sign);
        this.englishNavigationLetter.setOnClickListener(keyboardEnglishNavigationOnClick);
        this.englishNavigationSign.setOnClickListener(keyboardEnglishNavigationOnClick);
    }

    /**
     * 内容区域需要的布局参数
     **/
    public RelativeLayout.LayoutParams getKeyBoardContentParams() {
        return new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    }

    /**
     * 根据布局id返回一个绘制的view
     **/
    public View getLayoutView(int resourceId) {
        return LayoutInflater.from(getContext()).inflate(resourceId, null);
    }
}
